package com.peng.linkedlist;

import org.junit.Test;

public class SomeTests {

    /**
     * 反转单链表
     */
    @Test
    public void reverseLinkedListTest() {
             /*
        单链表
         */
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        Node newHead = reverseLinkedList(node1);

        System.out.println();
    }

    /**
     * 反转双链表
     */
    @Test
    public void reverseDoubleLinkedListTest() {
        DoubleNode node1 = new DoubleNode(1);
        DoubleNode node2 = new DoubleNode(2);
        DoubleNode node3 = new DoubleNode(3);
        DoubleNode node4 = new DoubleNode(4);
        DoubleNode node5 = new DoubleNode(5);

        node1.next = node2;

        node2.next = node3;
        node2.prev = node1;

        node3.next = node4;
        node3.prev = node2;

        node4.next = node5;
        node4.prev = node3;


        DoubleNode newHead = reverseDoubleLinkedList(node1);
        System.out.println(newHead);


    }

    /**
     * 单链表删除指定值的节点
     */
    @Test
    public void removeValueNodeLinkedListTest() {
             /*
        单链表
         */
        Node node1 = new Node(3);
        Node node2 = new Node(2);
        Node node3 = new Node(2);
        Node node4 = new Node(4);
        Node node5 = new Node(3);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        Node node = removeValueNode(node1, 3);
        System.out.println();

    }

    private Node removeValueNode(Node head, int numVal) {
        if (head == null) {
            return null;
        }

        //考虑开头一串就都是要被删除的值，
        //这种情况可以直接移动到不需要被删除的第一个位置，然后把前面的一串直接抛掉即可
        while (head != null) {
            if (head.value != numVal) {
                break;
            }
            head = head.next;
        }

        Node pre = head;
        Node cur = head;

        while (cur!=null){
            if(cur.value==numVal){
                pre.next = cur.next;
            }else{
                pre = cur;
            }
            cur = cur.next;
        }

        return head;


    }


    private DoubleNode reverseDoubleLinkedList(DoubleNode head) {
        if (head == null) {
            return null;
        }
        DoubleNode next;
        DoubleNode pre = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            head.prev = next;

            pre = head;
            head = next;
        }


        return pre;
    }


    /**
     * 反转链表
     *
     * @param head
     * @return
     */
    public static Node reverseLinkedList(Node head) {
        if (head == null) {
            return null;
        }

        Node next;
        Node pre = null;
        while (head != null) {
            next = head.next;
            head.next = pre;

            pre = head;
            head = next;
        }

        return pre;


    }

}
